package com.xonro.common.histrans.sfeg.biz.base;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.emm.util.DateUtil;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.consts.DrpConst;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.sfeg.biz.common.SfegRecordTransBiz;
import com.xonro.common.histrans.sfeg.ctx.SfecProcessCreateContext;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class BaseProjectBiz extends SfegRecordTransBiz {
    @Override
    protected void transProcess0(SfecProcessCreateContext historyTransContext) {
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();

        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable("BO_XR_MDM_PROJECT");//项目基础信息
        logModel.setOuterTable("tb_xmxx");//基础项目信息表
        logModel.setOuterPK(mainData.getString("xmbh"));
        logModel.setOuterExt1(mainData.getString("xmmc"));
        logModel.setOuterExt2(mainData.getString("xmlb"));//项目类别
        BOCreateData mainBOCreateData = new BOCreateData();
        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);
        mainBO.set("APPLY_DATE", DateUtil.parse( mainData.getString("applyDate"), "yyyy-MM-dd"));
        mainBO.set("PROJECT_NO", mainData.getString( "xmbh"));//项目编号
        mainBO.set("PROJECT_NAME", mainData.getString("xmmc"));//费用类型编码
        mainBO.set("PROJECT_SITE", mainData.getString("xmdz"));//项目地址
        mainBO.set("PROJECT_NATURE",getProjectNature( mainData.getString("xmxz")));//项目性质
        mainBO.set("PROJECT_STATUS", mainData.getString("xmzt"));//项目状态
        mainBO.set("ABBREVIATION",mainData.getString("xmjc"));//项目简称
        mainBO.set("BRIEF_DESC",mainData.getString("xmjj"));//项目简介
        mainBO.set("IS_ENTITY",mainData.getString("isstxm")=="0"?"1":"0");//是否实体 0是 1否
        mainBO.set("IS_SCIENTIFIC",mainData.getString("iskeyan")=="0"?"1":"0");//是否科研 0是 1否
        mainBO.set("IS_STRADDLE_YEAR",mainData.getString("ishtbhkn")=="0"?"1":"0");//合同编号是否跨年 0是 1否
        mainBO.set("CONTRACT_PATTERN", getMainMode(mainData.getString("xmzt")));//主合同模式
        mainBO.set("TAX_TYPE", mainData.getString("taxfs"));//计税方式
        mainBO.set("MAIN_CONTENT", mainData.getString("xmzj"));//总工作量(万元)
        mainBO.set("CONTRACT_PRICE", mainData.getString("xmzj"));//项目造价(万元)
        if(StringUtils.isNotBlank(mainData.getString("jhkgrq"))&&mainData.getString("jhkgrq").length()==10) {
            mainBO.set("PLAN_START_DATE", DateUtil.parse(mainData.getString("jhkgrq"), "yyyy-MM-dd"));//计划开工日期
        }
        if(StringUtils.isNotBlank(mainData.getString("jhjgrq"))&&mainData.getString("jhjgrq").length()==10) {
            mainBO.set("PLAN_END_DATE", DateUtil.parse(mainData.getString("jhjgrq"), "yyyy-MM-dd"));//计划竣工日期
        }
        if(StringUtils.isNotBlank(mainData.getString("sjkgrq"))&&mainData.getString("sjkgrq").length()==10) {
            mainBO.set("REAL_START_DATE", DateUtil.parse(mainData.getString("sjkgrq"), "yyyy-MM-dd"));//实际开工日期
        }
        if(StringUtils.isNotBlank(mainData.getString("sjjgrq"))&&mainData.getString("sjjgrq").length()==10) {
            mainBO.set("REAL_END_DATE", DateUtil.parse(mainData.getString("sjjgrq"), "yyyy-MM-dd"));//实际竣工日期
        }
        mainBO.set("COMPANY_NAME", mainData.getString("jsdw"));//建设单位
        mainBO.set("LINKMAN", mainData.getString("jsdwLxr"));//建设单位联系人  单位类别 001
        mainBO.set("MOBILE", mainData.getString("jsdwLxdh"));//建设单位联系电话
        mainBO.set("COMPANY_NAME", mainData.getString("sjdw"));//设计单位
        mainBO.set("LINKMAN", mainData.getString("sjdwLxr"));//设计单位联系人
      //项目基础信息子表 BO_XR_MDM_PROJECT_COMPANY
        setCompanyData(historyTransContext, createUser, bindId, boCreateDataList, mainData,mainData.getString("jsdw"),mainData.getString("jsdwLxr"),mainData.getString("jsdwLxdh"),"001");
        setCompanyData(historyTransContext, createUser, bindId, boCreateDataList, mainData,mainData.getString("sjdw"),mainData.getString("sjdwLxr"),mainData.getString("sjdwLxdh"),"002");
        setCompanyData(historyTransContext, createUser, bindId, boCreateDataList, mainData,mainData.getString("jldw"),mainData.getString("jldwLxr"),mainData.getString("jldwLxdh"),"003");
        setCompanyData(historyTransContext, createUser, bindId, boCreateDataList, mainData,mainData.getString("zbdw"),mainData.getString("zbdwLxr"),mainData.getString("zbdwLxdh"),"004");
        //主要实物量
       List<RowMap> contentData=SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_CCID).getMaps("SELECT * FROM tb_xmswy  WHERE xmbh = ?  ",mainData.getString("xmbh"));
       for(RowMap content:contentData){
           String uuid = UUIDGener.getUUID();
           BOCreateData subBOCreateData = new BOCreateData();
           boCreateDataList.add(subBOCreateData);
           subBOCreateData.setCreateUser(createUser);
           subBOCreateData.setBoName("BO_XR_MDM_PROJECT_CONTENT");
           BO subBO = new BO();
           subBO.setId(uuid);
           subBOCreateData.setBo(subBO);
           subBO.set("CONTENT",content.getString("swnr"));
           subBO.set("UNIT",content.getString("swdw"));
           subBO.set("COUNT", content.getString("swsl"));
           super.addLogModel(historyTransContext, "tb_xmswy", content.getString("id"), "BO_XR_MDM_PROJECT_CONTENT", uuid, bindId);
       }
        historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);


        //添加附件
        FileTransData fileTransData = new FileTransData();
        fileTransData.setBoId(mainBOId);
        //OA表订单编号
        fileTransData.setFkCode(mainData.getString("xmbh"));
        fileTransData.setProcessInstId(bindId);
        historyTransContext.getFileTransDatas().add(fileTransData);
        mainBOCreateData.setBoName("BO_XR_MDM_PROJECT");
        fileTransData.setBoName("BO_XR_MDM_PROJECT");
        fileTransData.setFieldName("ATTACHMENT");

    }

    private void setCompanyData(SfecProcessCreateContext historyTransContext, String createUser, String bindId, List<BOCreateData> boCreateDataList, RowMap mainData, String companyName, String linkman, String mobile, String type) {
        String uuid = UUIDGener.getUUID();
        BOCreateData subBOCreateData = new BOCreateData();
        boCreateDataList.add(subBOCreateData);
        subBOCreateData.setCreateUser(createUser);
        subBOCreateData.setBoName("BO_XR_MDM_PROJECT_COMPANY");
        BO subBO = new BO();
        subBO.setId(uuid);
        subBOCreateData.setBo(subBO);
        subBO.set("COMPANY_NAME", companyName);
        subBO.set("LINKMAN",linkman);
        subBO.set("MOBILE", mobile);
        subBO.set("COMPANY_TYPE",type);
        super.addLogModel(historyTransContext, "tb_xmxx", mainData.getString("xmbh")+type, "BO_XR_MDM_PROJECT_COMPANY", uuid, bindId);
    }


    private String getMainMode(String code){
        if(StringUtils.isBlank(code)){
            return "";
        }
        String param="";
        switch(code) {
            case "zhtms01":
                param="001";
                break;
            case "zhtms02":
                param="003";
                break;
           default:
               param="";

        }
        return param;
    }

    private String getProjectNature(String nature){
        if(StringUtils.isBlank(nature)){
            return "";
        }
        String param="";
        switch(nature){
            case "szgc":
               param= "2";
               break;
            case "xmxzqt":
                param="3";
                break;
             default:
                 param="2";

        }
        return  param;
    }

    @Override
    protected String getProcessDefId() {
        return "obj_a76dc4b6e6414d208d597f7299f8d1d5";
    }

    @Override
    protected RowMap getMainData() {
        return null;
    }

    @Override
    protected String getActivityDefId() {
        return null;
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        RowMap mainData = ctx.getMainData();
        String projectNo=mainData.getString("xmbh");
        String projectName=mainData.getString("xmmc");
        return projectName+projectNo;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {
        RowMap mainData = ctx.getMainData();
        return mainData.getString("xmbh");
    }

    @Override
    protected String getOuterTable(SfecProcessCreateContext historyTransContext) {
        return "tb_xmxx";
    }


    @Override
    protected String getPk(SfecProcessCreateContext historyTransContext) {
        RowMap mainData = historyTransContext.getMainData();
        return mainData.getString("xmbh");
    }

    @Override
    protected List<RowMap> getMainDatas() {
        //查询实施单位是康益海洋的公司
        return SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_CCID).getMaps("SELECT * FROM tb_xmxx  WHERE ssdw = '1040'  ");
    }

}
